package work.timejay.user.service.books.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import work.timejay.common.ApiPageEntity;
import work.timejay.common.SuperServiceImpl;
import work.timejay.user.dto.books.BookTypePageDTO;
import work.timejay.user.entity.books.BookType;
import work.timejay.user.entity.books.UserBooks;
import work.timejay.user.mapper.books.BookTypeMapper;
import work.timejay.user.service.books.BookTypeService;
import org.springframework.stereotype.Service;
import work.timejay.user.vo.books.BookTypePageVO;
import work.timejay.utils.FileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * book类型表 服务实现类
 * </p>
 *
 * @author 王文杰
 * @since 2023-11-23
 */
@Service
public class BookTypeServiceImpl extends SuperServiceImpl<BookTypeMapper, BookType> implements BookTypeService {

    @Autowired
    private FileUtils fileUtils;

    @Override
    public ApiPageEntity<BookTypePageVO> bookPage(BookTypePageDTO dto) {
        Page<BookType> iPage = lambdaQuery()
                .page(new Page<>(dto.getPageNum(), dto.getPageSize()));
        List<BookTypePageVO> voList = new ArrayList<BookTypePageVO>();
        iPage.getRecords().forEach(e -> {
            BookTypePageVO bean = BookTypePageVO.createReturnEntity(e, fileUtils.preview(e.getBookPath()));
            voList.add(bean);
        });
        return ApiPageEntity.create(iPage, voList);
    }

    @Override
    public BookType findBookTypeByType(Long type) {
        return lambdaQuery()
                .eq(BookType::getType, type)
                .one();
    }
}
